//
//  HMVisitorLoginView.swift
//  Weibo
//
//  Created by HM on 16/5/20.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit
//导入命名空间
import SnapKit


//1. 声明协议
// NSObjectProtocol 继承基协议  可以使用weak 关键字
@objc protocol HMVisitorLoginViewDelegate: NSObjectProtocol {
    //声明协议方法 
    
    //用户的登陆的协议方法 
    optional func userWilllogin()
    //用户注册的协议方法
    func userWiilRegister()
    
}

class HMVisitorLoginView: UIView {

    //声明代理属性
    //代理对象 必须使用弱引用  在swift属性的默认就是强引用
    weak var visitorDelegate: HMVisitorLoginViewDelegate?
    
    //MARK: 通过外界调用设置访客视图的页面信息
    func updateUI(title: String, imageName: String,isHome: Bool = false) {
        if  isHome {
            //是首页  做动画
            startAnimation()
        } else {
            //不是首页
            largeHouse.hidden = true
            //将圈圈一道最顶层
            bringSubviewToFront(circleIcon)
        }
        circleIcon.image = UIImage(named: imageName)
        tipLabel.text = title
    }
    
    
    //给圈圈添加动画效果
    private func startAnimation() {
        let basic = CABasicAnimation(keyPath: "transform.rotation")
        //基本设置
        //动画时间
        basic.duration = 15
        //动画的重复次数
        basic.repeatCount = MAXFLOAT
        basic.toValue = 2 * M_PI
        //当动画结束 或者 视图处于非活跃状态 动画效果不移除图层
        basic.removedOnCompletion = false
        //给图层添加动画效果
        circleIcon.layer.addAnimation(basic, forKey: nil)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    //默认实现报错
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置UI界面
    private func setupUI() {
        addSubview(circleIcon)
        addSubview(backIcon)
        addSubview(largeHouse)
        addSubview(tipLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)

        //设置最大布局宽度
        tipLabel.preferredMaxLayoutWidth = 230
        
        circleIcon.snp_makeConstraints { (make) in
            //make 就是circleIcon 取消了frame布局的对象
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-80)
        }
        
        largeHouse.snp_makeConstraints { (make) in
            //            make.centerY.equalTo(circleIcon.snp_centerY)
            //            make.centerX.equalTo(circleIcon.snp_centerX)
            make.center.equalTo(circleIcon.snp_center)
        }
        
        //提示文案
        tipLabel.snp_makeConstraints { (make) in
            make.top.equalTo(circleIcon.snp_bottom).offset(16)
            make.centerX.equalTo(circleIcon.snp_centerX)
            //约束宽度
            //            make.width.equalTo(230)
            //约束高度 解决页面跳动的问题
            make.height.equalTo(40)
        }
        
        loginBtn.snp_makeConstraints { (make) in
            make.left.equalTo(tipLabel.snp_left)
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
            
        }
        
        registerBtn.snp_makeConstraints { (make) in
            make.right.equalTo(tipLabel.snp_right)
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
        }
        
        backIcon.snp_makeConstraints { (make) in
            make.center.equalTo(circleIcon.snp_center)
        }
        
        //一般使用sRGB取值 
        let value: CGFloat = 237.0 / 255.0
        backgroundColor = UIColor(red: value, green: value, blue: value, alpha: 1)
        
        
        //添加按钮的点击时间
        loginBtn.addTarget(self, action: #selector(HMVisitorLoginView.loginBtnDidClick), forControlEvents: .TouchUpInside)
        registerBtn.addTarget(self, action: #selector(HMVisitorLoginView.registerBtnDidClick), forControlEvents: .TouchUpInside)
        
        
        
        
    }
    
    
    //MARK: 按钮点击事件
    @objc private func loginBtnDidClick() {
        
       //使用代理属性 调用协议方法
        // '?' 判断代理对象是否为空
        //userWilllogin? 是否响应协议方法
        visitorDelegate?.userWilllogin?()
    }
    
    
    @objc private func registerBtnDidClick() {
        visitorDelegate?.userWiilRegister()
    }
    
    //懒加载子视图
    //遮罩图片
    private lazy var backIcon: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //圈圈
    private lazy var circleIcon: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //大房子
    private lazy var largeHouse: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //提示登录注册的 '文案'
    private lazy var tipLabel: UILabel = UILabel(title: "关注一些人,回到这里看看有什么惊喜", font: 14, textColor: UIColor.darkGrayColor())
        
        
        
//        {
//        let l = UILabel()
//        
//        l.text = "关注一些人,回到这里看看有什么惊喜"
//        //字号 和颜色
//        l.textColor = UIColor.darkGrayColor()
//        l.font = UIFont.systemFontOfSize(14)
//        l.textAlignment = .Center
//        //设置自动换行
//        l.numberOfLines = 0
//        //最大的布局宽度
//        
//        //自适应大小
//        l.sizeToFit()
//        return l
//    }()
    
    //登录注册按钮
    private lazy var loginBtn: UIButton = {
        let btn = UIButton()
        
        btn.setTitle("登录", forState: .Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        //设置文字大小
        btn.titleLabel?.font = UIFont.systemFontOfSize(15)
        let image = UIImage(named: "common_button_white_disable")!
        let capW = Int(image.size.width * 0.5)
        let capH = Int(image.size.height * 0.5)
        btn.setBackgroundImage(image.stretchableImageWithLeftCapWidth(capW, topCapHeight: capH), forState: .Normal)
        
        return btn
    }()
    
    private lazy var registerBtn: UIButton = {
        let btn = UIButton()
        
        btn.setTitle("注册", forState: .Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        //设置文字大小
        btn.titleLabel?.font = UIFont.systemFontOfSize(15)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        
        return btn
    }()
    

}
